package com.geek.javase.ch18.videodemo.reflect.field;

import java.lang.reflect.Field;

/*
 *@Date 2025/11/5
 *@Author wangpeng
 *@Description
 *  *  * Field中常用方法
 *  * 1.设置值
 *  * void set(Object obj, Object value)
 *  * void setInt(Object obj, int i)
 *  * void setLong(Object obj, long l)
 *  * void setBoolean(Object obj, boolean z)
 *  * void setDouble(Object obj, double d)
 *  *
 *  * 2.获取值
 *  * Object get(Object obj)
 *  * int  getInt(Object obj)
 *  * long getLong(Object obj)
 *  * boolean getBoolean(Object ob)
 *  * double getDouble(Object obj)
 *  *
 *  * 3.void setAccessible(true);暴力反射，设置为true可以取消访问权限检查
 *  * 4.Class getType(); 获取属性的类型，返回Class对象
 *  *
 *  * 总结步骤：
 *  * 1）、先获取类类型，即Class对象
 *  * 2）、获取属性Field对象，利用getDeclaredField()、getDeclaredFields()
 *  * 3）、setAccessible(true)设置取消访问权限检查
 *  * 4）、引用数据类型：set()、get()
 *  *     基本数据类型：setXxx()、getXxx()
 */
public class FieldDemo2 {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        //1.获取Employee 类类型
        Class<Employee> clazz = Employee.class;
        Employee employee = clazz.newInstance();

        //2.获取Field属性，设置属性的值
        Field nameField = clazz.getDeclaredField("name");
        nameField.setAccessible(true);
        nameField.set(employee, "wp");

        Field salaryField = clazz.getDeclaredField("salary");
        salaryField.setAccessible(true);
        salaryField.set(employee, 10000);

        Field countField = clazz.getDeclaredField("count");
        countField.setInt(employee, 10);

        //3.获取属性的值
        System.out.println(employee);
        System.out.println(nameField.get(employee));
        /*System.out.println(salaryField.get(employee));
        System.out.println(countField.get(employee));*/
        System.out.println(salaryField.getDouble(employee));
        System.out.println(countField.getInt(employee));
    }

}
